/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.core.index;

import com.inspur.edp.lcm.metadata.api.entity.MetadataPackage;
import com.inspur.edp.lcm.metadata.common.Utils;
import com.inspur.edp.lcm.metadata.core.entity.MdProjDependencySource;
import com.inspur.edp.lcm.metadata.core.entity.MdProjectDependencyNode;
import com.inspur.edp.lcm.metadata.devcommon.ManagerUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Set;

@Slf4j
public class MetadataPackageIndexServiceForPackages extends MetadataPackageIndexService {

    private static MetadataPackageIndexServiceForPackages instance;

    public static MetadataPackageIndexServiceForPackages getInstance() {
        if (instance == null) {
            instance = new MetadataPackageIndexServiceForPackages();
        }
        return instance;
    }

    /**
     * 根据元数据包名获取对应package依赖引用节点
     * @param metadataPackageName 元数据包名
     * @return 对应依赖引用节点
     */
    public MdProjectDependencyNode getMetadataDependencyNode(String metadataPackageName) {
        String packagePath = ManagerUtils.getMetadataPackageLocation();
        MdProjectDependencyNode cacheNode = getNodeFromCache(packagePath, metadataPackageName);
        // package 保存目录名为 元数据包名 + .mdpkg
        String dir = metadataPackageName + Utils.getMetadataPackageExtension();
        MdProjectDependencyNode fileNode = getDependencyNodeInfoFromDir(packagePath, dir, false);

        if (fileNode == null) {
            removeNodeFromCache(packagePath, metadataPackageName);
            return null;
        }

        // 若文件节点与缓存节点不一致
        // 根据文件构造元数据包对象,并获取其元数据编码列表 以及元数据包的依赖信息
        if (!fileNode.equals(cacheNode)) {
            cacheNode = fileNode;

            // 记录来源
            cacheNode.setSourceName(MdProjDependencySource.Package);

            // 将缓存中的节点填充 其依赖节点以及工程内元数据编码列表 内容
            String packageLocation = fileServiceImp.getDirectoryName(cacheNode.getLocation());
            MetadataPackage metadataPackageInfo = metadataManager.getMetadataPackageInfo(cacheNode.getMetadataPackageName(), packageLocation);

            // 获取元数据包中的metadataIds
            List<String> idsInMdPkg = getMetadataIdsInMdPkg(metadataPackageInfo);
            cacheNode.setMetadataIds(idsInMdPkg);

            // 补充元数据包引用
            Set<String> metadataRefsInMdPkg = getMetadataRefsInMdPkg(metadataPackageInfo);
            cacheNode.setMetadataPackageRefs(metadataRefsInMdPkg);

            // 补充数据库数据包引用
            Set<String> mdpkgRefsInMdPkg = getMdpkgRefsInMdPkg(metadataPackageInfo);
            cacheNode.setMdpkgRefs(mdpkgRefsInMdPkg);

            // 补充IDP元数据引用
            Set<String> idpMetadataIds = getIdpMdIdsInMdPkg(metadataPackageInfo);
            cacheNode.setIdpMetadataIds(idpMetadataIds);

            // 更新缓存中依赖节点
            saveNodeToCache(packagePath, metadataPackageName, cacheNode);
        }
        return cacheNode;
    }
}
